/********************
 * Source Facts Panel
 */
AIR2.Source.Facts = function() {
    return new AIR2.UI.Panel({
        colspan: 1,
        title: 'Facts',
        iconCls: 'air2-source-facts-icon',
        storeData: AIR2.Source.SFDATA,
        url: AIR2.Source.BASEURL + '/facts',
        itemSelector: '.air2-source-facts',
        tpl: new Ext.XTemplate(
            '<div class="air2-source-facts">',
             '<table>',
             '<tpl for=".">',
              '<tr>',
                '<td><span class="fact-name">{[values.Fact.fact_name]}</span></td>',
                '<td>{[this.factValue(values)]}</td>',
              '</tr>',
             '</tpl>',
             '</table>',
            '</div>',
            { 
                compiled: true,
                disableFormats: true, 
                factValue: function(values){
                    var ret = '--';
                    if ( values.SourceFV ) {
                        ret = values.SourceFV.fv_value;
                    }
                    else if (values.sf_src_value) {
                        ret = values.sf_src_value; 
                    }
                    else if ( values.AnalystFV ) {
                        ret = values.AnalystFV.fv_value;
                    }
                    return ret;
                }
            }
        ),        
        editModal: {
            allowAdd: AIR2.Source.SUMMDATA.authz.may_write,
            layout: null,
            items: [{
                xtype: 'box',
                cls: 'header',
                html: '<div class="air2-source-factseditor-header air2-source-facts">' +
                       '<div class="fact-name-header fact-header" style="width:160px">Field</div>' +
                       '<div class="fact-header" style="width:150px">Analyst (Selected)</div>' +
                       '<div class="fact-header" style="width:150px">Source (Selected)</div>' +
                       '<div class="fact-header" style="width:200px">Source (Explanation)</div>' +
                      '</div>'
            },{
                xtype: 'air2liveeditor',
                url: AIR2.Source.BASEURL + '/facts',
                pageSize: 50,
                allowEdit: true,
                allowDelete: true,
                hideButtons: true,
                editForm: {
                    labelAlign: 'top',
                    items: {
                        xtype: 'compositefield',
                        hideLabel: true,
                        items: [
                            {
                                xtype: 'air2combo',
                                fieldLabel: 'Fact Name',
                                name: 'fact_uuid',
                                store: new Ext.data.JsonStore({
                                    data: AIR2.Source.FLDATA
                                }),
                                valueField: 'fact_uuid',
                                displayField: 'fact_name',
                                emptyText: 'Select a Fact',
                                width: 160,
                                listeners: {
                                    select: function(box, rec) {
                                        // disable the next 3 boxes
                                        var analyst = box.nextSibling();
                                        var source = analyst.nextSibling();
                                        var rawtext = source.nextSibling();
                                        analyst.disable();
                                        source.disable();
                                        rawtext.disable();

                                        // mapped fact or raw text
                                        var factId = rec.data.fact_identifier;
                                        if (Ext.isDefined(AIR2.Facts[factId])) {
                                            analyst.store.loadData(AIR2.Facts[factId]);
                                            analyst.enable();

                                            // reselect or reset
                                            if (box.disabled) {
                                                analyst.setValue(analyst.getValue());
                                            }
                                            else {
                                                analyst.reset();
                                                analyst.onTriggerClick();
                                            }
                                        }
                                        else {
                                            rawtext.enable();

                                            // reselect or reset
                                            if (box.disabled) {
                                                rawtext.setValue(rawtext.getValue());
                                            }
                                            else {
                                                rawtext.reset();
                                            }
                                        }
                                    }
                                }
                            },
                            {
                                xtype: 'air2combo',
                                fieldLabel: 'Analyst selected',
                                name: 'sf_fv_id',
                                store: [['foo','bar']], // not actually used but is prototype -- see 'beforeedit' below
                                emptyText: 'Analyst-selected value',
                                disabled: true,
                                allowBlank: false,
                                width: 150
                            },
                            {
                                xtype: 'air2combo',
                                fieldLabel: 'Source selected',
                                name: 'sf_src_fv_id',
                                store: [['foo','bar']],   // ignored since this is read-only
                                valueField: 'fv_id',
                                displayField: 'fv_value',
                                width: 150,
                                emptyText: 'Source-selected value',
                                disabled: true,
                                allowBlank: false,
                                readOnly: true   // hides trigger too
                            },
                            {
                                xtype: 'textfield',
                                name: 'sf_src_value',
                                disabled: true,  // cannot edit Source-originated data
                                allowBlank: false,
                                emptyText: 'Source-authored value',
                                width: 200
                            }
                        ]
                    },
                    listeners: {
                        beforeedit: function(form, rec) {
                            var f = form.getForm();

                            // setup fact field (re-init store each time)
                            var factFld = f.findField('fact_uuid');
                            factFld.store.removeAll();
                            factFld.store.loadData(AIR2.Source.FLDATA);
                            rec.store.each(function(fvRec) {
                                if (rec.phantom && !fvRec.phantom) {
                                    var rmv = factFld.store.getById(fvRec.data.fact_uuid);
                                    factFld.store.remove(rmv);
                                }
                            });
                            factFld.setDisabled(!rec.phantom);

                            // set the selections
                            var sel = factFld.store.getById(rec.data.fact_uuid);
                            if (sel) factFld.onSelect(sel);
                        },
                        beforesave: function(formpanel, rec) {
                            // make sure we don't set any blank keys
                            for (var key in rec.data) {
                                if (!rec.data[key]) delete rec.data[key];
                            }
                        }
                    }
                },
                tpl: new Ext.XTemplate(
                    '<div class="air2-source-facts">' +
                      '<div class="fact-name" style="width:160px">{[values.Fact.fact_name]}</div>' +
                      '<div class="fact-analyst-selected" style="width:150px">{[this.selectedFV(values.AnalystFV)]}</div>' +
                      '<div class="fact-source-selected" style="width:150px">{[this.selectedFV(values.SourceFV)]}</div>' +
                      '<div class="fact_value" style="width:200px">{[this.srcSF(values.sf_src_value)]}</div>' +
                        '<button class="air2-rowedit"></button>' +
                        '<button class="air2-rowdelete"></button>' +
                    '</div>',
                    {
                        compiled: true,
                        disableFormats: true,
                        srcSF: function(fvalue) {
                            ( fvalue ) ? ret = fvalue : ret = '--';
                            return ret;
                        },

                        selectedFV: function(values) {
                            if ( values ) {
                                return values.fv_value;
                            }
                            else {
                                return "--";
                            }
                        }
                    }
                )
            }]
        }
    });
}